package sy.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * sql条件工具类
 * 
 * 查询条件分段；每段直接用下划线_分割；第一段以Q_开头，第二段是属性；第三段是操作符；第四段是字段类型
 * 
 * 示例请查看sy.test.util.TestQueryFilter类
 * 
 * @author 孙宇
 *
 */
public class QueryFilter {

	private static final Log log = Logs.get();

	private Criteria cri = Cnd.cri();

	public QueryFilter() {

	}

	public QueryFilter(HttpServletRequest request) {
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = request.getParameter(name);
			addFilter(name, value);
		}
	}

	public void addFilter(String column, String value) {
		if (Strings.isNotBlank(column) && column.toUpperCase().startsWith("Q_")) {
			String[] command = column.split("_");// 分割name
			if (command != null && (command.length == 3 || command.length == 4)) {
				if (command[2].equalsIgnoreCase(">") || command[2].equalsIgnoreCase(">=") || command[2].equalsIgnoreCase("<") || command[2].equalsIgnoreCase("<=")) {
					cri.where().and(command[1], command[2], Long.parseLong(value));
				} else if (command[2].equalsIgnoreCase("GT")) {
					cri.where().and(command[1], ">", Long.parseLong(value));
				} else if (command[2].equalsIgnoreCase("GE")) {
					cri.where().and(command[1], ">=", Long.parseLong(value));
				} else if (command[2].equalsIgnoreCase("LT")) {
					cri.where().and(command[1], "<", Long.parseLong(value));
				} else if (command[2].equalsIgnoreCase("LE")) {
					cri.where().and(command[1], "<=", Long.parseLong(value));
				} else if (command[2].equalsIgnoreCase("in")) {
					if (command[3].equalsIgnoreCase("L")) {
						cri.where().and(command[1], command[2], value);
					} else if (command[3].equalsIgnoreCase("S")) {
						cri.where().and(command[1], command[2], value.split(","));
					}
				} else if (command[2].equalsIgnoreCase("notIn")) {
					if (command[3].equalsIgnoreCase("L")) {
						cri.where().and(command[1], "not in", value);
					} else if (command[3].equalsIgnoreCase("S")) {
						cri.where().and(command[1], "not in", value.split(","));
					}
				} else if (command[2].equalsIgnoreCase("like")) {
					cri.where().and(command[1], command[2], value);
				} else if (command[2].equalsIgnoreCase("notLike")) {
					cri.where().and(command[1], "not like", value);
				} else if (command[2].equalsIgnoreCase("=") || command[2].equalsIgnoreCase("!=")) {
					cri.where().and(command[1], command[2], value);
				} else if (command[2].equalsIgnoreCase("EQ")) {
					cri.where().and(command[1], "=", value);
				} else if (command[2].equalsIgnoreCase("NE")) {
					cri.where().and(command[1], "!=", value);
				} else if (command[2].equalsIgnoreCase("is")) {
					if (Strings.isBlank(value)) {
						cri.where().and(command[1], command[2], null);
					} else {
						cri.where().and(command[1], command[2], value);
					}
				} else if (command[2].equalsIgnoreCase("isNot")) {
					if (Strings.isBlank(value)) {
						cri.where().and(command[1], "is not", null);
					} else {
						cri.where().and(command[1], "is not", value);
					}
				}
			}
		}
	}

	public Criteria getCondition() {
		return cri;
	}

}
